package rabbit.delay;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/7/31 14:56
 */


public class consumer {
    private static final String EXCHANGE_NAME = "hiddencallbasedelayedexchange";// "outerhostexchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("39.106.17.245");
        //factory.setHost("192.168.0.88");
        factory.setPort(5672);
        //factory.setHost("39.106.144.161");
        //factory.setHost("192.168.0.201");//
//        factory.setVirtualHost("OuterHost");
//        factory.setUsername("admin");
        factory.setVirtualHost("OuterHost");
        factory.setUsername("root");
        factory.setPassword("root");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        channel.exchangeDeclare(EXCHANGE_NAME, "x-delayed-message", true, false, args);
        //channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true);
        //String queueName = "hiddencallbasedelayedqueue";//"housequeue";
        String queueName = "hiddencallbasedelayedqueue";//"housequeue";
        //channel.queueBind(queueName, EXCHANGE_NAME, "eallTopic");//"houseTopic");
        channel.queueBind(queueName, EXCHANGE_NAME, "hiddencallbasedelayedqueue");
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
                //message=RSAHelper.decryptionByPublicKey(message,"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCj6y+SBwoJvn584PMKVUuw/tc8IVbrhXR3JlAYWFMpZfdS6sbcpN12nmD//wfl9SqhCbivZuPWbhaat2ILNYYjbtjBurlLqJ7nu8AVPGsMU80lJ1Jr/q1fJTYyEVX9dXS45Me/lwGta58Dv+PKRsrk9EME7azAhi1yAtavftbhwIDAQAB");
                //System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }
}